package com.example.administrator.financialmanager;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.financialmanager.dao.UserDao;
import com.example.administrator.financialmanager.model.Tb_user;

public class MainActivity extends AppCompatActivity {
    private EditText login_account_input;
    public static String username;
    private EditText login_password_input;
    private String password;
    private Button login_admit;
    private Button login_reset;
    UserDao userDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        //调试代码；此部分代码仅供前端调试使用，实际运行时请打开生产代码；

//        Intent intent = new Intent(MainActivity.this, TotalChartActivity.class);
//        startActivity(intent);

       //生产代码；项目投入生产时，务必打开此部分代码，并关闭所有无关调试代码
        login_account_input = findViewById(R.id.login_account_input);
        login_password_input = findViewById(R.id.login_password_input);

        login_admit = findViewById(R.id.login_admit);
        login_reset = findViewById(R.id.login_reset);
        userDao = new UserDao(MainActivity.this);


        login_admit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = login_account_input.getText().toString();
                password = login_password_input.getText().toString();
                if ("".equals(username) || "".equals(password)) {
                    //调试代码；此部分代码仅供前端调试使用，实际运行时请打开生产代码；
                    Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username",username);
                    bundle.putString("password",password);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    //生产代码；项目投入生产时，务必打开此部分代码，并关闭所有无关调试代码
//                    Toast.makeText(MainActivity.this, "用户名和密码不能为空！",
//                            Toast.LENGTH_SHORT).show();
                }else {
                    if (userDao.find(new Tb_user(username,password))) {
                        Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("username",username);
                        bundle.putString("password",password);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        //启动HomeActivity
                    } else {
                        Toast.makeText(MainActivity.this, "用户名和密码错误",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        login_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}
